local screen_x, screen_y = guiGetScreenSize()
local lp = getLocalPlayer()
local HUD_FONT = nil
local NEWS_ANIMATION_A = 255
local NEWS_BAR_A = 85
local MESSAGE 
local MESSAGE_W
local NEWS_ANIMATION_X
local NEWS_ANIMATION_STATE = false

local NEWS_WARTESCHLANGE = {}

addEventHandler("onClientResourceStart", getResourceRootElement(), function()
	showPlayerHudComponent("all", false)
	showPlayerHudComponent("radar", true)
	--showChat(false)
	
	start_load()
end)

function start_load()
	HUD_FONT = dxCreateFont("FILES/HUD_FONT.TTF", 17)
	HUD_FONT_HP = dxCreateFont("FILES/HUD_FONT.TTF", 10)
	addEventHandler("onClientRender", getRootElement(), DATEBOX_RENDER)
	addEventHandler("onClientRender", getRootElement(), HP_RENDER)
	addEventHandler("onClientRender", getRootElement(), AP_RENDER)
	addEventHandler("onClientRender", getRootElement(), VEH_RENDER)
end

addEvent("NEW_HUD_MESSAGE", true)
addEventHandler("NEW_HUD_MESSAGE", getRootElement(), function(MESSAGE_EVENT)
	if NEWS_ANIMATION_STATE then table.insert(NEWS_WARTESCHLANGE, MESSAGE_EVENT) return end
	NEWS_ANIMATION_STATE = true
	addEventHandler("onClientRender", getRootElement(), NEWS_RENDER_BOX)
	MESSAGE_W = dxGetTextWidth(MESSAGE_EVENT, 1, HUD_FONT)
	MESSAGE = MESSAGE_EVENT
	NEWS_ANIMATION_X = screen_x + 1
	NEWS_BAR_A = 0
	setTimer(function() NEWS_BAR_A = NEWS_BAR_A + 5 end, 100, 17)
	setTimer(function() addEventHandler("onClientRender", getRootElement(), NEWS_RENDER_TEXT) end, 1700, 1)
end)

function DATEBOX_RENDER()
	local DATE_TABLE = getRealTime()
	local TIME_HOUR = DATE_TABLE.hour
	local TIME_MINUTE = DATE_TABLE.minute
	local DATE_DAY = DATE_TABLE.monthday
	local DATE_MONTH = 1 + DATE_TABLE.month
	local DATE_YEAR = 1900 + DATE_TABLE.year
	if DATE_DAY < 10 then
		DATE_DAY = "0"..DATE_DAY
	end
	if DATE_MONTH < 10 then
		DATE_MONTH = "0"..DATE_MONTH
	end
	if TIME_HOUR < 10 then
		TIME_HOUR = "0"..TIME_HOUR
	end
	if TIME_MINUTE < 10 then
		TIME_MINUTE = "0"..TIME_MINUTE
	end
	
	local TIME = TIME_HOUR..":"..TIME_MINUTE
	local DATE = DATE_DAY.."."..DATE_MONTH.."."..DATE_YEAR
	local SCHRIFT_1_W = dxGetTextWidth(TIME, 1, HUD_FONT)
	local SCHRIFT_2_W = dxGetTextWidth(DATE, 1, HUD_FONT)
	
	dxDrawText(TIME, (screen_x - 20) - 82.5 - SCHRIFT_1_W/2 - 1, screen_y - 90 - 1, screen_x, screen_y, tocolor(0, 0, 0), 1, HUD_FONT)
	dxDrawText(TIME, (screen_x - 20) - 82.5 - SCHRIFT_1_W/2, screen_y - 90, screen_x, screen_y, tocolor(255, 255, 255), 1, HUD_FONT)
	
	dxDrawText(DATE, (screen_x - 20) - 82.5 - SCHRIFT_2_W/2  - 1, screen_y - 55 - 1, screen_x, screen_y, tocolor(0, 0, 0), 1, HUD_FONT)
	dxDrawText(DATE, (screen_x - 20) - 82.5 - SCHRIFT_2_W/2 , screen_y - 55, screen_x, screen_y, tocolor(255, 255, 255), 1, HUD_FONT)
	
	dxDrawRectangle(screen_x - 185 - 1, screen_y - 94 - 1, 165, 74, tocolor(0,0,0,61.2))
	dxDrawRectangle(screen_x - 185, screen_y - 94, 165, 74, tocolor(211,211,211,61.2))
	
	dxDrawLine(screen_x - 165,  screen_y - 62 - 1, screen_x - 40, screen_y - 62 - 1, tocolor(0, 0, 0, 211), 2)
	dxDrawLine(screen_x - 165,  screen_y - 62, screen_x - 40, screen_y - 62, tocolor(255, 255, 255, 211), 2)
end

function NEWS_RENDER_TEXT()
	dxDrawText(MESSAGE, NEWS_ANIMATION_X, 1, screen_x, screen_y, tocolor(255,255,255, NEWS_ANIMATION_A), 1, HUD_FONT)
	
	if NEWS_ANIMATION_X > (0 - MESSAGE_W + 1) then
		NEWS_ANIMATION_X = NEWS_ANIMATION_X - 3
	else
		removeEventHandler("onClientRender", getRootElement(), NEWS_RENDER_TEXT)
		setTimer(function() NEWS_BAR_A = NEWS_BAR_A - 5 end, 100, 17)
		setTimer(function() removeEventHandler("onClientRender", getRootElement(), NEWS_RENDER_BOX) 
		
		MESSAGE_W = 0
		NEWS_ANIMATION_X = 0
		NEWS_ANIMATION_STATE = false
		if #NEWS_WARTESCHLANGE >= 1 then
			for index, news in ipairs(NEWS_WARTESCHLANGE) do
				triggerEvent("NEW_HUD_MESSAGE", getRootElement(), news)
				table.remove(NEWS_WARTESCHLANGE, index)
				return
			end
		end
		
		end, 1700, 1)
	end
end

function NEWS_RENDER_BOX()
	dxDrawRectangle(0, 0, screen_x, 30, tocolor(211, 211, 211, NEWS_BAR_A))
end

function HP_RENDER()
	local HP = math.floor(getElementHealth(getLocalPlayer()))
	local FONT_W = dxGetTextWidth("HP: ", 1, HUD_FONT_HP)
	dxDrawText("HP: ", screen_x - 185 - 1, screen_y - 120 - 1, screen_x, screen_y, tocolor(0,0,0), 1, HUD_FONT_HP)
	dxDrawText("HP: ", screen_x - 185, screen_y - 120, screen_x, screen_y, tocolor(211,211,211), 1, HUD_FONT_HP)
	
	if HP > 100 then setElementHealth(getLocalPlayer(), 100) end
	local HP_MAX_BREITE = 165 - FONT_W
	local HP_BREITE = HP_MAX_BREITE / 100 * HP
	dxDrawRectangle(screen_x - 185 + FONT_W - 1, screen_y - 116 - 1, HP_MAX_BREITE, 10, tocolor(0, 0, 0, 211))
	dxDrawRectangle(screen_x - 185 + FONT_W, screen_y - 116, HP_BREITE, 10, tocolor(255, 50, 0, 211))
	
end

function AP_RENDER()
	local AP = math.floor(getPedArmor(getLocalPlayer()))
	
	
	if AP > 100 then setElementHealth(getLocalPlayer(), 100) end
	if AP > 0 then
		local FONT_W = dxGetTextWidth("AP: ", 1, HUD_FONT_HP)
		dxDrawText("AP: ", screen_x - 185 - 1, screen_y - 140 - 1, screen_x, screen_y, tocolor(0,0,0), 1, HUD_FONT_HP)
		dxDrawText("AP: ", screen_x - 185, screen_y - 140, screen_x, screen_y, tocolor(211,211,211), 1, HUD_FONT_HP)
	
		local AP_MAX_BREITE = 165 - FONT_W
		local AP_BREITE = AP_MAX_BREITE / 100 * AP
		dxDrawRectangle(screen_x - 185 + FONT_W - 1, screen_y - 136 - 1, AP_MAX_BREITE, 10, tocolor(0, 0, 0, 211))
		dxDrawRectangle(screen_x - 185 + FONT_W, screen_y - 136, AP_BREITE, 10, tocolor(211, 211, 211, 211))
	end
end

function VEH_RENDER()
	if getPedOccupiedVehicle(lp) then
		local VEH = math.floor(getElementHealth(getPedOccupiedVehicle(getLocalPlayer())))
		
		if VEH > 0 then
			local FONT_W = dxGetTextWidth("VEH: ", 1, HUD_FONT_HP)
			dxDrawText("VEH: ", screen_x - 185 - 1, screen_y - 160 - 1, screen_x, screen_y, tocolor(0,0,0), 1, HUD_FONT_HP)
			dxDrawText("VEH: ", screen_x - 185, screen_y - 160, screen_x, screen_y, tocolor(211,211,211), 1, HUD_FONT_HP)
		
			local VEH_MAX_BREITE = 165 - FONT_W
			local VEH_BREITE = VEH_MAX_BREITE / 1000 * VEH
			local VEH_COLOR_R, VEH_COLOR_G, VEH_COLOR_B  = getVehicleColor(getPedOccupiedVehicle(getLocalPlayer()), true)
			if VEH_COLOR_R <= 10 and VEH_COLOR_G <= 10 and VEH_COLOR_B <= 10 then VEH_COLOR_R = 50 VEH_COLOR_G = 50 VEH_COLOR_B = 50 end
			dxDrawRectangle(screen_x - 185 + FONT_W - 1, screen_y - 156 - 1, VEH_MAX_BREITE, 10, tocolor(0, 0, 0, 211))
			dxDrawRectangle(screen_x - 185 + FONT_W, screen_y - 156, VEH_BREITE, 10, tocolor(VEH_COLOR_R, VEH_COLOR_G, VEH_COLOR_B, 211))
		end
	end
end